<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Area;
use app\common\model\Address as AddressModel;
use think\Db;

/**
 * aaa
 * 前台首页控制器
 * @package app\index\controller
 */
class Address extends Home
{
    public $noNeedLogin = ['get_province','get_city','get_area'];

    public function tes()
    {
        echo '222';
    }

    public function ttt(){
        echo  '111';
    }
    // 根据省份获取城市
    public function get_province()
    {
        $city = Area::where('level', '1')->column('id,name'); //数据
        $city = format_linkage($city);
        $this->result('success', $city, 200);
    }

    // 根据省份获取城市
    public function get_city($province_id = '')
    {
        $city = Area::where('level', '2')->where('pid', $province_id)->column('id,name'); //数据
        $city = format_linkage($city);
        $this->result('success', $city, 200);
    }

    // 根据城市获取地区
    public function get_area($city_id = '')
    {
        $area = Area::where('level', '3')->where('pid', $city_id)->column('id,name'); //数据
        $area = format_linkage($area);
        $this->result('success', $area, 200);
    }

    //获取用户地址列表
    public function lists(){
        $userInfo = $this->userInfo;
        $data = AddressModel::where(['user_id'=>$userInfo['id']])->paginate();
        foreach ($data as $key=>$value){
            $value['province'] = Area::where(['id'=>$value['pro_id']])->value('name');
            $value['city'] = Area::where(['id'=>$value['city_id']])->value('name');
            $value['county'] = Area::where(['id'=>$value['dist_id']])->value('name');
        }
        $this->result('查询成功',$data,200);
    }

    /**
     * 添加收货地址
     */
    public function add(){
        if($this->request->isPost()){
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            $validate = new \app\api\validate\Address();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['user_id'] = $userInfo['id'];
            //参数有默认地址的时候修改其他默认的为2
            if($getData['is_check'] == 1){
                $invoiceData = AddressModel::where(['user_id'=>$userInfo['id'],'is_check'=>1])->select();
                if($invoiceData){
                    foreach ($invoiceData as $key=>$value){
                        AddressModel::where(['id'=>$value['id']])->update(['is_check'=>2]);
                    }
                }
            }
            $find = AddressModel::where(['user_id'=>$userInfo['id']])->find();
            if(empty($find)){
                $getData['is_check'] == 1;
            }
            $insert = AddressModel::create($getData);
            if($insert){
                $this->result('添加成功','',200);
            }
            $this->result('添加失败，请重试','',204);
        }
        $this->result('无效的请求','',204);
    }

    /**
     * 删除收货地址
     */
    public function del(){
        if($this->request->isPost()){
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $del = AddressModel::where(['id'=>$getData['id'],'user_id'=>$userInfo['id']])->delete();
            if($del){
                $this->result('删除成功','',200);
            }
            $this->result('删除失败，请重试','',204);
        }
        $this->result('无效的请求','',204);
    }

    /**
     * 更新收货地址
     */
    public function save(){
        if($this->request->isPost()){
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            $validate = new \app\api\validate\Address();
            if (!$validate->scene('update')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            //参数有默认抬头的时候修改其他默认的为2
            if($getData['is_check'] == 1){
                $invoiceData = AddressModel::where(['user_id'=>$userInfo['id'],'is_check'=>1])->select();
                if($invoiceData){
                    foreach ($invoiceData as $key=>$value){
                        AddressModel::where(['id'=>$value['id']])->update(['is_check'=>2]);
                    }
                }
            }
            $update = AddressModel::where(['id'=>$getData['id'],'user_id'=>$userInfo['id']])->update($getData);
            if($update){
                $this->result('更新成功','',200);
            }
            $this->result('更新失败，请重试','',204);
        }else{
            $getData = $this->request->get();
            if(empty($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $data = AddressModel::where(['user_id'=>$userInfo['id'],'id'=>$getData['id']])->find();
            if(empty($data)){
                $this->result('查询成功','',200);
            }else{
                $data['province'] = Area::where(['id'=>$data['pro_id']])->value('name');
                $data['city'] = Area::where(['id'=>$data['city_id']])->value('name');
                $data['county'] = Area::where(['id'=>$data['dist_id']])->value('name');
            }
            $this->result('查询成功',$data,200);

        }
        $this->result('无效的请求','',204);
    }

    /**
     * 查找用户默认收货地址
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDefault(){
        $userInfo = $this->userInfo;
        $data = Db::name('qt_address')->where(['user_id'=>$userInfo['id'],'is_check'=>1])->find();
        if(!empty($data)){
            $data['province'] = Area::where(['id'=>$data['pro_id']])->value('name');
            $data['city'] = Area::where(['id'=>$data['city_id']])->value('name');
            $data['county'] = Area::where(['id'=>$data['dist_id']])->value('name');
        }
        $this->result('查询成功',$data,200);
    }



}
